package com.xbongbong.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.I18nUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pojo.ProjectDTO;
import com.xbongbong.pojo.ProjectVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.service.ProjectService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 描述信息
 *
 * @ClassName ProjectController
 * @Author 梁鲁江
 * 创建时间 2022/6/16 14:27
 * 修改时间  2022/6/16 14:27
 **/
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/dolphin")
public class ProjectController {
    @Resource
    private ProjectService projectService;
    @RequestMapping(value = "/queryCodeByName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String queryCodeByName(@RequestBody ProjectDTO projectDTO, BindingResult br) {
        XbbResponse<ProjectVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // feign调用后国际化处理
                LocaleContextHolder.setLocale(I18nUtil.getLocale(projectDTO.getLocale()));
                Long code = projectService.queryCodeByName(projectDTO.getProjectName());
                ProjectVO projectVO = new ProjectVO();
                projectVO.setCode(code);
                response = new XbbResponse<>(projectVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
         
    }
}
